package com.cuotoniu.lazybug.core;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import com.cuotoniu.lazybug.core.servlet.ResourceServlet;


/**
 * LazyBugInitializer:
 * initial the Context
 * 
 * @author HLJ
 *
 */
public class LazyBugInitializer implements WebApplicationInitializer{

	public void onStartup(ServletContext context) throws ServletException {
		/** create the annotation context */
		/** AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); */
		/** scan the data config class package */
		/**
		root.scan("com.cuotoniu.lazybug.core.config.data");
		context.addListener(new ContextLoaderListener(root));
		*/
		
		/** create the annotation context */
		AnnotationConfigWebApplicationContext annotationContext = new AnnotationConfigWebApplicationContext();
		/** registered the LazyBugConfig */
		annotationContext.setConfigLocation("com.cuotoniu.lazybug.core.config.web"); 
		
		/** Registered Servlet of the ResourceServlet  */
		ServletRegistration.Dynamic defaultServlet = context.addServlet("resource", new ResourceServlet());
		
		defaultServlet.setLoadOnStartup(1);		/** startup level */
		defaultServlet.addMapping(new String[]{"*.jpg","*.js","*.css","*.html"});	/** match the url */
		
		/** Registered Servlet of the DispatcherServlet  */
		ServletRegistration.Dynamic dispatch = context.addServlet("dispatcher", new DispatcherServlet(annotationContext));
		
		dispatch.setLoadOnStartup(2);	/** startup level */
		dispatch.addMapping("/");		/** match the url */
		
	}
	
}
